package com.pengcheng.test;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DsendUtil {
    @Autowired
    private Common common;
    @Autowired
    private JsonUtil jsonUtil;
    public String get_msg(JSONObject row_dsendtask)
    {
        String msg="";
        double flowrate=jsonUtil.get_double_by_path(row_dsendtask.toJSONString(),"$.row_dlog.flowrate");
        flowrate=flowrate/1000;
        double flow=jsonUtil.get_double_by_path(row_dsendtask.toJSONString(),"$.row_dlog.flow");
        String sn=jsonUtil.get_string_by_path(row_dsendtask.toJSONString(),"$.row_dlog.row_device.field_60");
        sn=sn.substring(0,10);
        String ddate=jsonUtil.get_string_by_path(row_dsendtask.toJSONString(),"$.row_dlog.ddate");
        if(flowrate==-99||flow==-99||common.is_empty(sn))
        {
            throw new RuntimeException("获取信息失败");
        }
        msg="691A68B3";
        msg+=decode_sn(sn);
        msg+="C0";
        msg+=decode_flowrate(flowrate);
        msg+=decode_flow(flow);
        msg+=decode_ddate(ddate);
        String crc=common.calculate_old(common.hex2byte(msg.substring(6)));
        msg+=crc;
        msg+="16";
        //计算crc
        return msg;
    }
    public String decode_sn(String sn)
    {
        String result=sn.substring(0,6);
        String index=Integer.toHexString(Integer.parseInt(sn.substring(6,10))).toUpperCase();
        log.info("index:"+index);
        while(index.length()<4)
        {
            index="0"+index;
        }
        result+=index.substring(2,4);
        result+=index.substring(0,2);
        return result;
    }
    public String decode_flowrate(double flowrate)
    {
        flowrate=flowrate*1000;
        String result=String.format("%.0f", flowrate);
        while (result.length()<9)
        {
            result="0"+result;
        }
        if(flowrate<0)
        {
            result="F"+result;
        }
        else {
            result="0"+result;
        }
        result=(result.substring(8,10)+result.substring(6,8)+result.substring(4,6)+result.substring(2,4)+result.substring(0,2));
        return result;
    }
    public String decode_flow(double flow)
    {
        String result=String.format("%.0f", Math.abs(flow));
        while (result.length()<10)
        {
            result="0"+result;
        }
        String high=result.substring(0,1);
        log.info("high:"+high);
        result=result.substring(1,10);
        String bstr=Integer.toBinaryString(Integer.parseInt(high));
        while (bstr.length()<4)
        {
            bstr="0"+bstr;
        }
        log.info("bstr:"+bstr);
        bstr=bstr.substring(0,3);
        log.info("bstr:"+bstr);
        if(flow<0)
        {
            bstr="1"+bstr;
        }
        else {
            bstr="0"+bstr;
        }
        log.info("bstr:"+bstr);
        bstr=Integer.toHexString(Integer.parseInt(bstr,2));
        log.info("bstr:"+bstr);
        result=bstr+result;
        result=(result.substring(8,10)+result.substring(6,8)+result.substring(4,6)+result.substring(2,4)+result.substring(0,2));
        return result;
    }
    public String decode_ddate(String ddate)
    {
        String result="";
        result+="0000";
        //年
        result+=ddate.substring(2,4);//年
        result+=ddate.substring(5,7);//月
        result+=ddate.substring(17,19);//秒
        result+=ddate.substring(14,16);//分
        result+=ddate.substring(11,13);//时
        result+=ddate.substring(8,10);//日
        result+="03";
        return result;
    }
}
